/**
 * 
 */
package lu.med.praxiswahl.wahlumed.client.addressbook;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.GregorianCalendar;

import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.model.beans.DoctorsTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.DoctorServiceInt;

/**
 * @author PatrickPierre
 * 
 */
public class AddressBookEditDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3090462625332595262L;

	private JPanel generalDataPan, contactPan;
	private JLabel lblVorname, lblUcmCode, lblName;
	private JTextField nameTextField;
	private JTextField vornameTextField;
	private JTextField ucmTextField;

	private JLabel lblFax;

	private JLabel lblEmail;

	private JLabel lblTelefon;
	private JTextField telefonTextField;
	private JTextField faxTextField;
	private JTextField emailTextField;
	private JTextField websiteTextField;
	private JLabel lblAddresse;
	private JTextField addressTextField;
	private JLabel errorLabel;
	private JTextField fachGebietTextField;
	private JLabel lblFachgebiet;

	private Long entryId = -1l;
	
	private Main dataManager;

	public AddressBookEditDialog(Main dataManager) {
		super();
		this.dataManager = dataManager;
		getContentPane().setBackground(new Color(192, 192, 192));
		this.initComponents();
		this.display();
	}

	public void initComponents() {

		this.contactPan = new JPanel();
		contactPan.setBackground(new Color(192, 192, 192));
		contactPan.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Kontakt",
				TitledBorder.CENTER, TitledBorder.TOP, null, Color.DARK_GRAY));
		contactPan.setBounds(285, 11, 226, 158);
		this.contactPan.setLayout(null);
		getContentPane().setLayout(null);
		getContentPane().add(this.contactPan);

		lblTelefon = new JLabel("Telefon:");
		lblTelefon.setFont(new Font("Verdana", Font.PLAIN, 11));
		lblTelefon.setBounds(10, 27, 50, 19);
		contactPan.add(lblTelefon);

		lblFax = new JLabel("Fax:");
		lblFax.setFont(new Font("Verdana", Font.PLAIN, 11));
		lblFax.setBounds(10, 57, 50, 19);
		contactPan.add(lblFax);

		lblEmail = new JLabel("E-Mail:");
		lblEmail.setFont(new Font("Verdana", Font.PLAIN, 11));
		lblEmail.setBounds(10, 87, 50, 19);
		contactPan.add(lblEmail);

		JLabel lblWebseite = new JLabel("Webseite:");
		lblWebseite.setBackground(new Color(169, 169, 169));
		lblWebseite.setFont(new Font("Verdana", Font.PLAIN, 11));
		lblWebseite.setBounds(10, 117, 57, 19);
		contactPan.add(lblWebseite);

		telefonTextField = new JTextField();
		telefonTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		telefonTextField.setColumns(10);
		telefonTextField.setBounds(78, 26, 138, 20);
		contactPan.add(telefonTextField);

		faxTextField = new JTextField();
		faxTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		faxTextField.setColumns(10);
		faxTextField.setBounds(78, 57, 138, 20);
		contactPan.add(faxTextField);

		emailTextField = new JTextField();
		emailTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		emailTextField.setColumns(10);
		emailTextField.setBounds(78, 87, 138, 20);
		contactPan.add(emailTextField);

		websiteTextField = new JTextField();
		websiteTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		websiteTextField.setColumns(10);
		websiteTextField.setBounds(78, 117, 138, 20);
		contactPan.add(websiteTextField);

		this.generalDataPan = new JPanel();
		generalDataPan.setBackground(new Color(192, 192, 192));
		generalDataPan.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Allgemeine Daten",
				TitledBorder.CENTER, TitledBorder.TOP, null, Color.DARK_GRAY));
		generalDataPan.setBounds(10, 11, 257, 158);
		getContentPane().add(generalDataPan);
		this.generalDataPan.setLayout(null);

		lblName = new JLabel("Name:");
		lblName.setFont(new Font("Verdana", Font.PLAIN, 12));
		lblName.setBounds(10, 27, 55, 20);
		generalDataPan.add(lblName);

		lblVorname = new JLabel("Vorname:");
		lblVorname.setFont(new Font("Verdana", Font.PLAIN, 12));
		lblVorname.setBounds(10, 58, 61, 20);
		generalDataPan.add(lblVorname);

		lblUcmCode = new JLabel("UCM Code:");
		lblUcmCode.setFont(new Font("Verdana", Font.PLAIN, 12));
		lblUcmCode.setBounds(79, 118, 70, 20);
		generalDataPan.add(lblUcmCode);

		nameTextField = new JTextField();
		nameTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		nameTextField.setBounds(79, 28, 168, 20);
		generalDataPan.add(nameTextField);
		nameTextField.setColumns(10);

		vornameTextField = new JTextField();
		vornameTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		vornameTextField.setColumns(10);
		vornameTextField.setBounds(79, 59, 168, 20);
		generalDataPan.add(vornameTextField);

		ucmTextField = new JTextField();
		ucmTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		ucmTextField.setColumns(10);
		ucmTextField.setBounds(161, 119, 86, 20);
		generalDataPan.add(ucmTextField);

		fachGebietTextField = new JTextField();
		fachGebietTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		fachGebietTextField.setColumns(10);
		fachGebietTextField.setBounds(79, 89, 168, 20);
		generalDataPan.add(fachGebietTextField);

		lblFachgebiet = new JLabel("Gebiet:");
		lblFachgebiet.setFont(new Font("Verdana", Font.PLAIN, 12));
		lblFachgebiet.setBounds(10, 89, 61, 20);
		generalDataPan.add(lblFachgebiet);

		lblAddresse = new JLabel("Addresse:");
		lblAddresse.setFont(new Font("Verdana", Font.PLAIN, 12));
		lblAddresse.setBounds(10, 197, 69, 20);
		getContentPane().add(lblAddresse);

		addressTextField = new JTextField();
		addressTextField.setFont(new Font("Verdana", Font.PLAIN, 12));
		addressTextField.setColumns(10);
		addressTextField.setBounds(10, 223, 501, 33);
		getContentPane().add(addressTextField);

		JButton btnAbbrechen = new JButton("Abbrechen");
		btnAbbrechen.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/cancel.png")));
		btnAbbrechen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnAbbrechen.setFont(new Font("Verdana", Font.PLAIN, 12));
		btnAbbrechen.setBounds(303, 282, 99, 33);
		getContentPane().add(btnAbbrechen);

		JButton btnOk = new JButton("OK");
		btnOk.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/login.png")));
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				performOKButton();
			}
		});
		btnOk.setFont(new Font("Verdana", Font.PLAIN, 12));
		btnOk.setBounds(412, 282, 99, 32);
		getContentPane().add(btnOk);

		errorLabel = new JLabel("");
		errorLabel.setFont(new Font("Verdana", Font.PLAIN, 11));
		errorLabel.setForeground(new Color(220, 20, 60));
		errorLabel.setBounds(10, 264, 501, 14);
		getContentPane().add(errorLabel);

	}

	public void display() {

		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

		pack();
		setSize(560, 370);
		setLocation((int) (dim.getWidth() - this.getWidth()) / 2,
				(int) (dim.getHeight() - this.getHeight()) / 2);
		ImageIcon img = new ImageIcon(getClass().getResource(
				"/address_book.png"));
		this.setIconImage(img.getImage());
		setResizable(false);

		setVisible(true);

	}

	public void performOKButton() {

		if (this.nameTextField.getText() == null
				|| this.nameTextField.getText().equals("")
				|| this.nameTextField.getText() == "") {
			this.errorLabel.setText("Bitte geben Sie einen Name ein");
		} else {
			DoctorsTBL doc = new DoctorsTBL();
			
			if(this.entryId != -1){
				//update
				doc.setId(entryId);
			}else{
				// for create no id
			}
			
			doc.setNachname(this.nameTextField.getText());
			doc.setVorname(this.vornameTextField.getText());
			doc.setFachgebiet(this.fachGebietTextField.getText());
			doc.setUcmCode(this.ucmTextField.getText());
			doc.setTelefon(this.telefonTextField.getText());
			doc.setFax(this.faxTextField.getText());
			doc.setEmail(this.emailTextField.getText());
			doc.setWebsite(this.websiteTextField.getText());
			doc.setAdresse(this.addressTextField.getText());
			doc.setLastEditor(Main.loggedUser);
			doc.setLastEditTime(new GregorianCalendar());

			DoctorServiceInt docService = this.dataManager.getDoctorContext();
			docService.creatOrSaveAddressEntry(doc);
			this.dispose();
		}
	}

	public void fillComponents(DoctorsTBL doc) {
		this.nameTextField.setText(doc.getNachname());
		this.vornameTextField.setText(doc.getVorname());
		this.fachGebietTextField.setText(doc.getFachgebiet());
		this.ucmTextField.setText(doc.getUcmCode());
		this.telefonTextField.setText(doc.getTelefon());
		this.faxTextField.setText(doc.getFax());
		this.emailTextField.setText(doc.getEmail());
		this.websiteTextField.setText(doc.getWebsite());
		this.addressTextField.setText(doc.getAdresse());
		this.entryId = doc.getId();

		this.setAlwaysOnTop(true);
		this.setModal(true);
	}

	public Long getEntryId() {
		return entryId;
	}

	public void setEntryId(Long entryId) {
		this.entryId = entryId;
	}
	
}
